
#include <stdint.h>
#include <string.h>
#include "main.h"
#include "hal_flash.h"
#include "main.h"



//int16_t parameter_read_from_flash(uint16_t index)
//{
//  uint32_t address = FLASH_PARAM_START_ADDRESS + index;
//  return *(__IO int16_t*)address;
//}

// uint16_t *p = (uint16_t *)(USER_PAGE_ADDRESS + 1500);
//    
//    for(i = 0;i < 16;i++)
//    {
//        table[i] = p[i];
//    }
		
//uint16_t parameter_read_from_flash(uint16_t index)
//{
//	uint32_t address;
//	uint16_t  value;
//	address = FLASH_PARAM_START_ADDRESS + index;
//	value = *(vu16*)address;
//	//uint16_t *p = (uint16_t *)(FLASH_PARAM_START_ADDRESS + index);
//	//temp = *p;
//  return value;
//}

//从指定地址开始写入多个数据
//void FLASH_WriteMoreData(uint16_t Addr,uint16_t *writeData, uint16_t countToWrite)
//{
//  uint16_t dataIndex;
//  FLASH_EraseInitTypeDef FLASH_EraseInit;
//	uint32_t err;
//	uint16_t *p = writeData;
//	if(Addr + countToWrite * 2 >= FLASH_SIZE)
//  {
//    return;//非法地址
//  }
//	
//	FLASH_EraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
//	FLASH_EraseInit.PageAddress = FLASH_PARAM_START_ADDRESS;
//	FLASH_EraseInit.NbPages = 1;
//	
//	HAL_FLASH_Unlock();         //解锁写保护
//	HAL_FLASHEx_Erase(&FLASH_EraseInit, &err);
//	uint64_t  tmp;
//	 for(dataIndex = 0; dataIndex < countToWrite; dataIndex++)
//  {
//    tmp = p[dataIndex];
//		//HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, FLASH_PARAM_START_ADDRESS + dataIndex * 2, tmp);
//		HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, FLASH_PARAM_START_ADDRESS + Addr + dataIndex * 2, tmp);
//		
//		
//  }
//	 HAL_FLASH_Lock();//上锁写保护
//	
////  if(countToWrite * 2 >= FLASH_SIZE)
////  {
////    return;//非法地址
////  }
//// if((   () == 0)||(GetFlashWriteSource() > 2))
//// {
////	 return;
//// 
//// }
//// SetFlashWriteSource(0);
////  HAL_FLASH_Unlock();         //解锁写保护
////	HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
////   FLASH_PageErase(FLASH_PARAM_START_ADDRESS);
//  //FLASH_ErasePage(FLASH_PARAM_START_ADDRESS);//擦除这个扇区
//  
////  for(dataIndex = 0; dataIndex < countToWrite; dataIndex++)
////  {
////   // FLASH_ProgramHalfWord(FLASH_PARAM_START_ADDRESS + dataIndex * 2, writeData[dataIndex]);
////		HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, FLASH_PARAM_START_ADDRESS + dataIndex * 2, writeData[dataIndex]);
////		
////  }
////  
////  HAL_FLASH_Lock();//上锁写保护
//	
//}


//void STFlash_Write(uint32_t address, uint16_t *pbuf, uint16_t len)
//{
//	  u32 realaddress = FLASH_PARAM_START_ADDRESS + address;
//    FLASH_EraseInitTypeDef FLASH_EraseInit;
//    uint32_t err;
//    uint16_t *p = (uint16_t *)pbuf;
//    
//    FLASH_EraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
//    FLASH_EraseInit.PageAddress = FLASH_PARAM_START_ADDRESS;
//    FLASH_EraseInit.NbPages = 1;
//    
//    HAL_FLASH_Unlock();
//   
//    HAL_FLASHEx_Erase(&FLASH_EraseInit, &err);
//    
//    uint64_t tmp;
//    
//    for(int i = 0;i < len ;i++)
//    {
//        tmp = p[i];
//        HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, realaddress + i *2, tmp);
//    }
//    HAL_FLASH_Lock();
//}




